package com.gitee.wsl.struct.command

import com.gitee.wsl.struct.command.context.Context
import com.gitee.wsl.struct.command.model.command.CoreCliktCommand
import com.gitee.wsl.struct.command.model.command.main
import com.gitee.wsl.struct.command.model.parameters.arguments.argument
import com.gitee.wsl.struct.command.model.parameters.arguments.delegate.convert
import com.gitee.wsl.struct.command.model.parameters.options.delegate.option
import com.gitee.wsl.struct.command.model.parameters.options.tran.check
import com.gitee.wsl.struct.command.model.parameters.options.tran.transformAll
import com.gitee.wsl.struct.command.model.parameters.options.tran.transformValues
import com.gitee.wsl.struct.command.model.parameters.options.tran.validate
import com.gitee.wsl.struct.command.model.parameters.types.int


data class Quad(val a: Int, val b: Int, val c: Int, val d: Int)

class Cli : CoreCliktCommand() {
    override fun help(context: Context) = "Validation examples"

    val count by option(help = "A positive even number").int()
        .check("Should be a positive, even integer") { it > 0 && it % 2 == 0 }

    val biggerCount by option(help = "A number larger than --count").int()
        .validate {
            require(count == null || count!! < it) {
                "--bigger-count must be larger than --count"
            }
        }

    val quad by option(help = "A four-valued option")
        .int()
        .transformValues(4) { Quad(it[0], it[1], it[2], it[3]) }

    val sum by option(help = "All values will be added")
        .int()
        .transformAll { it.sum() }

    val url by argument(help = "A URL")
        .convert { it }

    override fun run() {
        echo("count: $count")
        echo("biggerCount: $biggerCount")
        echo("quad: $quad")
        echo("sum: $sum")
        echo("url: $url")
    }
}

fun main(args: Array<String>) = Cli().main(args)